@{
    ViewData["Title"] = "git Page";
}

<div class="layui-main site-inline">
    @Html.Partial("_toolmenu")
    <div class="site-content">
        <div class="asset-content entry-content" id="main-content" deep="8">

            <!-- div class="asset-body" -->
            <p>我每天使用 Git ，但是很多命令记不住。</p>

            <!-- /div -->
            <!-- div id="more" class="asset-more" -->
            <p>一般来说，日常使用只要记住下图6个命令，就可以了。但是熟练使用，恐怕要记住60～100个命令。</p>

            <p><img src="~/layui/images/bg2015120901.png" style="width:800px;"></p>

            <p>下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。</p>

            <blockquote>
                <ul>
                    <li>Workspace：工作区</li>
                    <li>Index / Stage：暂存区</li>
                    <li>Repository：仓库区（或本地仓库）</li>
                    <li>Remote：远程仓库</li>
                </ul>
            </blockquote>

            <h2>一、新建代码库</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 在当前目录新建一个Git代码库
</span>$ git init

<span class="token comment" spellcheck="true"># 新建一个目录，将其初始化为Git代码库
</span>$ git init <span class="token punctuation">[</span>project<span class="token operator">-</span>name<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 下载一个项目和它的整个代码历史
</span>$ git clone <span class="token punctuation">[</span>url<span class="token punctuation">]</span>
</code></pre>
            </blockquote>

            <h2>二、配置</h2>

            <p>Git的设置文件为<code>.gitconfig</code>，它可以在用户主目录下（全局配置），也可以在项目目录下（项目配置）。</p>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 显示当前的Git配置
</span>$ git config <span class="token operator">--</span>list

<span class="token comment" spellcheck="true"># 编辑Git配置文件
</span>$ git config <span class="token operator">-</span>e <span class="token punctuation">[</span><span class="token operator">--</span>global<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 设置提交代码时的用户信息
</span>$ git config <span class="token punctuation">[</span><span class="token operator">--</span>global<span class="token punctuation">]</span> user<span class="token punctuation">.</span>name <span class="token string">"[name]"</span>
$ git config <span class="token punctuation">[</span><span class="token operator">--</span>global<span class="token punctuation">]</span> user<span class="token punctuation">.</span>email <span class="token string">"[email address]"</span>
</code></pre>
            </blockquote>

            <h2>三、增加/删除文件</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 添加指定文件到暂存区
</span>$ git add <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token comment" spellcheck="true"># 添加指定目录到暂存区，包括子目录
</span>$ git add <span class="token punctuation">[</span>dir<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 添加当前目录的所有文件到暂存区
</span>$ git add <span class="token punctuation">.</span>

<span class="token comment" spellcheck="true"># 添加每个变化前，都会要求确认
</span><span class="token comment" spellcheck="true"># 对于同一个文件的多处变化，可以实现分次提交
</span>$ git add <span class="token operator">-</span>p

<span class="token comment" spellcheck="true"># 删除工作区文件，并且将这次删除放入暂存区
</span>$ git rm <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token comment" spellcheck="true"># 停止追踪指定文件，但该文件会保留在工作区
</span>$ git rm <span class="token operator">--</span>cached <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 改名文件，并且将这个改名放入暂存区
</span>$ git mv <span class="token punctuation">[</span>file<span class="token operator">-</span>original<span class="token punctuation">]</span> <span class="token punctuation">[</span>file<span class="token operator">-</span>renamed<span class="token punctuation">]</span>
</code></pre>
            </blockquote>

            <h2>四、代码提交</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 提交暂存区到仓库区
</span>$ git commit <span class="token operator">-</span>m <span class="token punctuation">[</span>message<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 提交暂存区的指定文件到仓库区
</span>$ git commit <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token operator">-</span>m <span class="token punctuation">[</span>message<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 提交工作区自上次commit之后的变化，直接到仓库区
</span>$ git commit <span class="token operator">-</span>a

<span class="token comment" spellcheck="true"># 提交时显示所有diff信息
</span>$ git commit <span class="token operator">-</span>v

<span class="token comment" spellcheck="true"># 使用一次新的commit，替代上一次提交
</span><span class="token comment" spellcheck="true"># 如果代码没有任何新变化，则用来改写上一次commit的提交信息
</span>$ git commit <span class="token operator">--</span>amend <span class="token operator">-</span>m <span class="token punctuation">[</span>message<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 重做上一次commit，并包括指定文件的新变化
</span>$ git commit <span class="token operator">--</span>amend <span class="token punctuation">[</span>file1<span class="token punctuation">]</span> <span class="token punctuation">[</span>file2<span class="token punctuation">]</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre>
            </blockquote>

            <h2>五、分支</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 列出所有本地分支
</span>$ git branch

<span class="token comment" spellcheck="true"># 列出所有远程分支
</span>$ git branch <span class="token operator">-</span>r

<span class="token comment" spellcheck="true"># 列出所有本地分支和远程分支
</span>$ git branch <span class="token operator">-</span>a

<span class="token comment" spellcheck="true"># 新建一个分支，但依然停留在当前分支
</span>$ git branch <span class="token punctuation">[</span>branch<span class="token operator">-</span>name<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 新建一个分支，并切换到该分支
</span>$ git checkout <span class="token operator">-</span>b <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 新建一个分支，指向指定commit
</span>$ git branch <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 新建一个分支，与指定的远程分支建立追踪关系
</span>$ git branch <span class="token operator">--</span>track <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> <span class="token punctuation">[</span>remote<span class="token operator">-</span>branch<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 切换到指定分支，并更新工作区
</span>$ git checkout <span class="token punctuation">[</span>branch<span class="token operator">-</span>name<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 切换到上一个分支
</span>$ git checkout <span class="token operator">-</span>

<span class="token comment" spellcheck="true"># 建立追踪关系，在现有分支与指定的远程分支之间
</span>$ git branch <span class="token operator">--</span><span class="token keyword">set</span><span class="token operator">-</span>upstream <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> <span class="token punctuation">[</span>remote<span class="token operator">-</span>branch<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 合并指定分支到当前分支
</span>$ git merge <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 选择一个commit，合并进当前分支
</span>$ git cherry<span class="token operator">-</span>pick <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 删除分支
</span>$ git branch <span class="token operator">-</span>d <span class="token punctuation">[</span>branch<span class="token operator">-</span>name<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 删除远程分支
</span>$ git push origin <span class="token operator">--</span>delete <span class="token punctuation">[</span>branch<span class="token operator">-</span>name<span class="token punctuation">]</span>
$ git branch <span class="token operator">-</span>dr <span class="token punctuation">[</span>remote<span class="token operator">/</span>branch<span class="token punctuation">]</span>
</code></pre>
            </blockquote>

            <h2>六、标签</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 列出所有tag
</span>$ git tag

<span class="token comment" spellcheck="true"># 新建一个tag在当前commit
</span>$ git tag <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 新建一个tag在指定commit
</span>$ git tag <span class="token punctuation">[</span>tag<span class="token punctuation">]</span> <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 删除本地tag
</span>$ git tag <span class="token operator">-</span>d <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 删除远程tag
</span>$ git push origin <span class="token punctuation">:</span>refs<span class="token operator">/</span>tags<span class="token operator">/</span><span class="token punctuation">[</span>tagName<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 查看tag信息
</span>$ git show <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 提交指定tag
</span>$ git push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 提交所有tag
</span>$ git push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token operator">--</span>tags

<span class="token comment" spellcheck="true"># 新建一个分支，指向某个tag
</span>$ git checkout <span class="token operator">-</span>b <span class="token punctuation">[</span>branch<span class="token punctuation">]</span> <span class="token punctuation">[</span>tag<span class="token punctuation">]</span>
</code></pre>
            </blockquote>

            <h2>七、查看信息</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 显示有变更的文件
</span>$ git status

<span class="token comment" spellcheck="true"># 显示当前分支的版本历史
</span>$ git log

<span class="token comment" spellcheck="true"># 显示commit历史，以及每次commit发生变更的文件
</span>$ git log <span class="token operator">--</span>stat

<span class="token comment" spellcheck="true"># 搜索提交历史，根据关键词
</span>$ git log <span class="token operator">-</span>S <span class="token punctuation">[</span>keyword<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示某个commit之后的所有变动，每个commit占据一行
</span>$ git log <span class="token punctuation">[</span>tag<span class="token punctuation">]</span> HEAD <span class="token operator">--</span>pretty<span class="token operator">=</span>format<span class="token punctuation">:</span><span class="token operator">%</span>s

<span class="token comment" spellcheck="true"># 显示某个commit之后的所有变动，其"提交说明"必须符合搜索条件
</span>$ git log <span class="token punctuation">[</span>tag<span class="token punctuation">]</span> HEAD <span class="token operator">--</span>grep feature

<span class="token comment" spellcheck="true"># 显示某个文件的版本历史，包括文件改名
</span>$ git log <span class="token operator">--</span>follow <span class="token punctuation">[</span>file<span class="token punctuation">]</span>
$ git whatchanged <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示指定文件相关的每一次diff
</span>$ git log <span class="token operator">-</span>p <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示过去5次提交
</span>$ git log <span class="token operator">-</span><span class="token number">5</span> <span class="token operator">--</span>pretty <span class="token operator">--</span>oneline

<span class="token comment" spellcheck="true"># 显示所有提交过的用户，按提交次数排序
</span>$ git shortlog <span class="token operator">-</span>sn

<span class="token comment" spellcheck="true"># 显示指定文件是什么人在什么时间修改过
</span>$ git blame <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示暂存区和工作区的差异
</span>$ git diff

<span class="token comment" spellcheck="true"># 显示暂存区和上一个commit的差异
</span>$ git diff <span class="token operator">--</span>cached <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示工作区与当前分支最新commit之间的差异
</span>$ git diff HEAD

<span class="token comment" spellcheck="true"># 显示两次提交之间的差异
</span>$ git diff <span class="token punctuation">[</span>first<span class="token operator">-</span>branch<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">[</span>second<span class="token operator">-</span>branch<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示今天你写了多少行代码
</span>$ git diff <span class="token operator">--</span>shortstat <span class="token string">"@@{0 day ago}"</span>

<span class="token comment" spellcheck="true"># 显示某次提交的元数据和内容变化
</span>$ git show <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示某次提交发生变化的文件
</span>$ git show <span class="token operator">--</span>name<span class="token operator">-</span>only <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示某次提交时，某个文件的内容
</span>$ git show <span class="token punctuation">[</span>commit<span class="token punctuation">]</span><span class="token punctuation">:</span><span class="token punctuation">[</span>filename<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示当前分支的最近几次提交
</span>$ git reflog
</code></pre>
            </blockquote>

            <h2>八、远程同步</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 下载远程仓库的所有变动
</span>$ git fetch <span class="token punctuation">[</span>remote<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 显示所有远程仓库
</span>$ git remote <span class="token operator">-</span>v

<span class="token comment" spellcheck="true"># 显示某个远程仓库的信息
</span>$ git remote show <span class="token punctuation">[</span>remote<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 增加一个新的远程仓库，并命名
</span>$ git remote add <span class="token punctuation">[</span>shortname<span class="token punctuation">]</span> <span class="token punctuation">[</span>url<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 取回远程仓库的变化，并与本地分支合并
</span>$ git pull <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 上传本地指定分支到远程仓库
</span>$ git push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token punctuation">[</span>branch<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 强行推送当前分支到远程仓库，即使有冲突
</span>$ git push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token operator">--</span>force

<span class="token comment" spellcheck="true"># 推送所有分支到远程仓库
</span>$ git push <span class="token punctuation">[</span>remote<span class="token punctuation">]</span> <span class="token operator">--</span>all
</code></pre>
            </blockquote>

            <h2>九、撤销</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 恢复暂存区的指定文件到工作区
</span>$ git checkout <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 恢复某个commit的指定文件到暂存区和工作区
</span>$ git checkout <span class="token punctuation">[</span>commit<span class="token punctuation">]</span> <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 恢复暂存区的所有文件到工作区
</span>$ git checkout <span class="token punctuation">.</span>

<span class="token comment" spellcheck="true"># 重置暂存区的指定文件，与上一次commit保持一致，但工作区不变
</span>$ git reset <span class="token punctuation">[</span>file<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 重置暂存区与工作区，与上一次commit保持一致
</span>$ git reset <span class="token operator">--</span>hard

<span class="token comment" spellcheck="true"># 重置当前分支的指针为指定commit，同时重置暂存区，但工作区不变
</span>$ git reset <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 重置当前分支的HEAD为指定commit，同时重置暂存区和工作区，与指定commit一致
</span>$ git reset <span class="token operator">--</span>hard <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 重置当前HEAD为指定commit，但保持暂存区和工作区不变
</span>$ git reset <span class="token operator">--</span>keep <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 新建一个commit，用来撤销指定commit
</span><span class="token comment" spellcheck="true"># 后者的所有变化都将被前者抵消，并且应用到当前分支
</span>$ git revert <span class="token punctuation">[</span>commit<span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># 暂时将未提交的变化移除，稍后再移入
</span>$ git stash
$ git stash pop
</code></pre>
            </blockquote>

            <h2>十、其他</h2>

            <blockquote>
                <pre class=" language-bash"><code class=" language-bash">
<span class="token comment" spellcheck="true"># 生成一个可供发布的压缩包
</span>$ git archive
</code></pre>
            </blockquote>

            <p>（完）</p>

            <!-- /div -->

        </div>
    </div>
</div>